package com.kzj.mls.test;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.kzj.mls.R;
import com.kzj.mls.activity.CropImageActivity;
import com.kzj.mls.activity.GBaseActivity;
import com.kzj.mls.beans.CropImageBean;
import com.kzj.mls.global.App;
import com.kzj.mls.util.BitmapUtils;
import com.kzj.mls.util.ImageUtil;
import com.kzj.mls.widget.dialog.CameraDialog;
import com.kzj.mls.widget.dialog.CameraDialogAction;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/26.
 */
public class TextCropImageActivity extends GBaseActivity {


    Button button_test;
    ImageView imageView_test;
    Button button_paizhao,button_paizhao_tuxiang;
    private Bitmap mBitmap;
    private static final int REQUEST_GALLERY = 21;
    public final int CAMERA = 10001;
    public Uri photoUri;
    private CameraDialog cameraDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_cropimage);
        findViewByIds();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void findViewByIds() {
        button_paizhao_tuxiang= (Button) findViewById(R.id.button_paizhao_tuxiang);
        button_paizhao_tuxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cameraDialog == null) {

                    cameraDialog = CameraDialog.create(getActivity(),
                            new CameraDialogAction() {

                                @Override
                                public void clickCamera() {

                                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    SimpleDateFormat timeStampFormat = new SimpleDateFormat(
                                            "yyyy_MM_dd_HH_mm_ss");
                                    String filename = timeStampFormat.format(new Date());
                                    ContentValues values = new ContentValues();
                                    values.put(MediaStore.Images.Media.TITLE, filename);

                                    photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                                    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

                                    startActivityForResult(intent, CAMERA);
                                }

                                @Override
                                public void clickAblum() {
                                    //先跳到发布页面
                                    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                                    startActivityForResult(intent, REQUEST_GALLERY);

                                }
                            });

                }
                cameraDialog.show();
            }
        });
        button_test= (Button) findViewById(R.id.button_test);button_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, REQUEST_GALLERY);



            }
        });
        button_paizhao= (Button) findViewById(R.id.button_paizhao);button_paizhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                SimpleDateFormat timeStampFormat = new SimpleDateFormat(
                        "yyyy_MM_dd_HH_mm_ss");
                String filename = timeStampFormat.format(new Date());
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, filename);

                photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

                startActivityForResult(intent, CAMERA);


            }
        });

        imageView_test= (ImageView) findViewById(R.id.imageView_test);



    }
    private Uri getUri() {


        //兼容性控制
        File file;
        if (Build.VERSION.SDK_INT > 8) {
            file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        } else {
            file = new File(Environment.getExternalStorageDirectory(), "Camera");
        }

        if (!file.exists()) { // 创建目录
            file.mkdirs();
        }


        String name = "temp_" + System.currentTimeMillis() + ".png";
        File file1 = new File(file, name);
//		file1.deleteOnExit();
 //       imagePath = file1.getAbsolutePath();
        Uri uri = Uri.fromFile(file1);
        return uri;
    }
    @Override
    protected void initDatas() {

    }

    @Override
    protected void fillArrayInListView() {

    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent resultIntent) {
        super.onActivityResult(requestCode, responseCode, resultIntent);
        switch (requestCode)
        {
            case REQUEST_GALLERY:
                String absPath = BitmapUtils.getFilePathFromUri(this, resultIntent.getData());
                Intent intent = new Intent(TextCropImageActivity.this, CropImageActivity.class);
                intent.putExtra(CropImageActivity.ImageUrlTag,absPath);
                intent.putExtra(CropImageActivity.ActivityTag,this.getLocalClassName());
                startActivity(intent);

                break;
            case CAMERA:
                if (resultIntent != null && resultIntent.getData() != null) {
                    photoUri = resultIntent.getData();
                }
                App.displayImageHttpOrFile(getImagePathFromCamera(resultIntent,TextCropImageActivity.this,photoUri),imageView_test, ImageUtil.OptionsNormal());

                break;
        }
    }
    public String getImagePathFromCamera(Intent resultIntent,Activity activity,Uri uri)
    {

        String[] proj = { MediaStore.MediaColumns.DATA };
        Cursor cursor = null;
        if(activity!=null){
//				@SuppressWarnings("deprecation")
            cursor = activity.managedQuery(uri, proj, null, null, null);
        }
//			Cursor cursor = actvity.managedQuery(uri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        String imagePath = cursor.getString(column_index);
        return imagePath;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    public void onEventMainThread(CropImageBean cropImageBean) {
        //图片回调涉及多个类，防止无关类加载
        if (cropImageBean.getActivityString().equals(this.getLocalClassName())) {
            App.displayImageHttpOrFile(cropImageBean.getImageUrl(), imageView_test, ImageUtil.OptionsNormal());
        }
    }
}
